package com.rupeng.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.rupeng.pojo.ClassesUser;
import com.rupeng.pojo.User;
import com.rupeng.service.ClassesUserService;
import com.rupeng.service.UserService;
import com.rupeng.util.AjaxResult;
import com.rupeng.util.CommonUtils;

@Controller
@RequestMapping("/classesUser")
public class ClassesUserController {
	@Autowired
	private ClassesUserService classesUserService;
	@Autowired
	private UserService userService;
	@RequestMapping("/update.do")
	public ModelAndView update(Long classesId) {
		ModelAndView modelAndView = new ModelAndView("classesUser/update");
		modelAndView.addObject("classesId", classesId);
		List<User> users = classesUserService.selectSecondListByFirstId(classesId);
		modelAndView.addObject("users", users);
		return modelAndView;
	}
	@RequestMapping("/delete.do")
	public @ResponseBody AjaxResult delete(Long classesId, Long userId) {
		ClassesUser classesUser = new ClassesUser();
		classesUser.setClassesId(classesId);
		classesUser.setUserId(userId);
		classesUser = classesUserService.selectOne(classesUser);
		classesUserService.delete(classesUser.getId());
		return AjaxResult.successInstance("已将该用户移出班级");
	}
	@RequestMapping(value="/add.do",method=RequestMethod.GET)
	public ModelAndView addPage(Integer curr, String param, Long classesId) {
		ModelAndView modelAndView = new ModelAndView("classesUser/add");
		if (curr==null) {
			curr = 1;
		}
		if (!CommonUtils.isEmpty(param)) {
			param = "%"+param+"%";
		} else {
			param = null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("param", param);
		PageInfo<User> pageInfo = userService.search(curr, 10, params);
		modelAndView.addObject("pageInfo", pageInfo);
		modelAndView.addObject("classesId", classesId);
		return modelAndView;
	}
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult addSubmit(Long classesId, Long userId) {
		//先判断当前要添加到班级的用户是学生还是老师
		User user = userService.selectOne(userId);
		//老师，只需要判断当前版本有没有这个老师就可以
		ClassesUser classesUser = new ClassesUser();
		if (user.getIsTeacher()) {
			classesUser.setClassesId(classesId);
			classesUser.setUserId(userId);
			if (classesUserService.isExisted(classesUser)) {
				return AjaxResult.errorInstance("此老师已存在于该班级中");
			}
		} else {
			classesUser = new ClassesUser();
            classesUser.setUserId(userId);
          //学生，判断此学生不能在任意一个班级里面
            if (classesUserService.isExisted(classesUser)) {
                return AjaxResult.errorInstance("此学生已归属于某个班级");
            }
		}
		classesUser = new ClassesUser(classesId, userId);
        classesUserService.insert(classesUser);
        return AjaxResult.successInstance("添加成功");
	}
}
